home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / PowerPlant / Everything / CEverythingDoc.cp < prev    next >
Encoding:
Text File  |  1998-10-11  |  9.4 KB  |  467 lines  |  [TEXT/CWIE]

  1. // CEverythingDoc.cp -- Document methods
  2.  
  3. #include "CEverythingDoc.h"
  4. #include "CEverythingData.h"
  5.  
  6. #include "DDocData.h"
  7. #include "CButtons.h"
  8. #include "DDocData.h"
  9. #include "CCheckboxes.h"
  10. #include "DDocData.h"
  11. #include "CRadios.h"
  12. #include "DDocData.h"
  13. #include "CEditText.h"
  14. #include "DDocData.h"
  15. #include "CStuff.h"
  16. #include "DDocData.h"
  17. #include "CBars.h"
  18. #include "EverythingCmds.h"
  19. #include "CModalButtons.h"
  20. #include "CModalCheckboxes.h"
  21. #include "CModalRadios.h"
  22. #include "CModalText.h"
  23. #include "CModalStuff.h"
  24. #include "CModalBars.h"
  25.  
  26. #include <LFile.h>
  27. #include <LPlaceHolder.h>
  28. #include <LPrintout.h>
  29. #include <LString.h>
  30. #include <LWindow.h>
  31. #include <TArrayIterator.h>
  32. #include <UWindows.h>
  33.  
  34. const ResIDT    prto_PrintView        = 201;
  35. const ResIDT    STRx_Untitled        = 128;
  36.  
  37. // ---------------------------------------------------------------------------
  38. //        • CEverythingDoc
  39. // ---------------------------------------------------------------------------
  40.  
  41. CEverythingDoc::CEverythingDoc(
  42.     LCommander    *inSuper)
  43.     : LSingleDoc(inSuper)
  44. {
  45.     mData = new CEverythingData();
  46. }
  47.  
  48. // ---------------------------------------------------------------------------
  49. //        • ~CEverythingDoc
  50. // ---------------------------------------------------------------------------
  51. //    Destructor
  52. //
  53.  
  54. CEverythingDoc::~CEverythingDoc()
  55. {
  56.     // Delete all SubCommanders - copied from LCommander
  57.     // This fixes a bug that existed in CW11 and prior.
  58.     // It appears that the bug is fixed in CW Pro 1,
  59.     // so this code probably is no longer needed.
  60.     TArrayIterator<LCommander*> iterator(mSubCommanders, LArrayIterator::from_End);
  61.     LCommander*        theSub;
  62.     while (iterator.Previous (theSub)) {
  63.         mSubCommanders.RemoveItemsAt (1, iterator.GetCurrentIndex());
  64.         delete theSub;
  65.     }
  66.     mWindow = nil;
  67.  
  68.     delete mData;
  69. }
  70.  
  71. //----------
  72. void
  73. CEverythingDoc::newFile()
  74. {
  75.     mData->NewData();
  76.  
  77.     MakeWindows();
  78.  
  79.     NameNewDoc();        // Set name of untitled window
  80. }
  81.  
  82. //----------
  83. void
  84. CEverythingDoc::openFile(
  85.     FSSpec        *inFileSpec)
  86. {
  87.     mData->OpenData (inFileSpec);
  88.  
  89.     MakeWindows();
  90.  
  91.     if (mWindow != nil) {
  92.         mWindow->SetDescriptor (inFileSpec->name);
  93.     }
  94.     mIsSpecified = true;
  95. }
  96.  
  97. // ---------------------------------------------------------------------------
  98. //        • MakeWindows
  99. // ---------------------------------------------------------------------------
  100.  
  101. void
  102. CEverythingDoc::MakeWindows()
  103. {
  104.     DDocData*        docData = mData->GetDocData ();
  105.  
  106.     mButtons = CButtons::CreateButtons(this, docData);
  107.     mCheckboxes = CCheckboxes::CreateCheckboxes(this, docData);
  108.     mRadios = CRadios::CreateRadios(this, docData);
  109.     mEditText = CEditText::CreateEditText(this, docData);
  110.     mStuff = CStuff::CreateStuff(this, docData);
  111.     mBars = CBars::CreateBars(this, docData);
  112.  
  113.     mWindow = mButtons;
  114. }
  115.  
  116. // ---------------------------------------------------------------------------
  117. //        • NameNewDoc
  118. // ---------------------------------------------------------------------------
  119. //    Name a new, untitled document window
  120. //
  121. //    Untitled windows start with "untitled", then "untitled 1",
  122. //    "untitled 2", etc. Old numbers are reused, so there won't be
  123. //    gaps in the numbering.
  124. //
  125. //    This routine uses a STR# resource to store the "untitled" string,
  126. //    which can be localized to different languages. The first string
  127. //    is "untitled" and the second is "untitled " (trailing space),
  128. //    which is used when appending a number to the name.
  129.  
  130. void
  131. CEverythingDoc::NameNewDoc()
  132. {
  133.     // Start with the default name("untitled")
  134.     Str255    name;
  135.     ::GetIndString(name, STRx_Untitled, 1);
  136.  
  137.     long    num = 0;
  138.     while (UWindows::FindNamedWindow(name) != nil) {
  139.  
  140.             // An existing window has the current name
  141.             // Increment counter and try again
  142.  
  143.         ::GetIndString(name, STRx_Untitled, 2);
  144.         num++;
  145.         Str15    numStr;
  146.         ::NumToString(num, numStr);
  147.         LString::AppendPStr(name, numStr);
  148.     }
  149.  
  150.     mWindow->SetDescriptor(name);        // Finally, set window title
  151. }
  152. // ---------------------------------------------------------------------------
  153. //        • IsModified
  154. // ---------------------------------------------------------------------------
  155. //    Return whether the Document has changed since the last save
  156.  
  157. Boolean
  158. CEverythingDoc::IsModified()
  159. {
  160.     mIsModified = mData->IsDirty();
  161.  
  162.     return mIsModified;
  163. }
  164.  
  165. // ---------------------------------------------------------------------------
  166. //        • DoAESave
  167. // ---------------------------------------------------------------------------
  168. //    Save Document in the specified file with the specified file type
  169. //
  170. //    If file type is fileType_Default, use the normal file type for
  171. //    this document
  172.  
  173. void
  174. CEverythingDoc::DoAESave(
  175.     FSSpec    &inFileSpec,
  176.     OSType    inFileType)
  177. {
  178.     mData->DoSaveAs(&inFileSpec);                // Write out data
  179.                                         // Change window name
  180.     mWindow->SetDescriptor(inFileSpec.name);
  181. }
  182.  
  183. //----------
  184. void
  185. CEverythingDoc::DoSave()
  186. {
  187.     mData->DoSave();
  188. }
  189.  
  190. //----------
  191. void
  192. CEverythingDoc::DoRevert()
  193. {
  194.     mData->DoRevert();
  195. }
  196.  
  197. // ---------------------------------------------------------------------------
  198. //        • DoPrint
  199. // ---------------------------------------------------------------------------
  200. //    Print the contents of the Document
  201.  
  202. void
  203. CEverythingDoc::DoPrint()
  204. {
  205.     LPrintout        *thePrintout = LPrintout::CreatePrintout(prto_PrintView);
  206.     LPlaceHolder    *textPlace = (LPlaceHolder *)
  207.                                     thePrintout->FindPaneByID('TBox');
  208. //!    textPlace->InstallOccupant(mTextView, atNone);
  209.  
  210.  
  211.     thePrintout->DoPrintJob();
  212.     delete thePrintout;
  213. }
  214.  
  215. //----------
  216. void    CEverythingDoc::DoModalButtons ()
  217. {
  218.     CModalButtons*        dialog = CModalButtons::CreateModalButtons (this, cmdModalButtons);
  219. }
  220.  
  221. //----------
  222. void    CEverythingDoc::FinishModalButtons (
  223.     void*        ioParam)
  224. {
  225.     CModalButtons*        dialog = (CModalButtons *)ioParam;
  226.  
  227.  
  228.     // post-invoke code
  229.  
  230.     delete dialog;
  231. }
  232.  
  233. //----------
  234. void    CEverythingDoc::DoModalCheckboxes ()
  235. {
  236.     DModalCheckboxesData*        data = new DModalCheckboxesData;
  237.  
  238.  
  239.     // pre-invoke code
  240.  
  241.     CModalCheckboxes*        dialog = CModalCheckboxes::CreateModalCheckboxes (this, cmdModalCheckboxes, data);
  242. }
  243.  
  244. //----------
  245. void    CEverythingDoc::FinishModalCheckboxes (
  246.     void*        ioParam)
  247. {
  248.     CModalCheckboxes*        dialog = (CModalCheckboxes *)ioParam;
  249.     DModalCheckboxesData*        data = dialog->GetData ();
  250.  
  251.  
  252.     // post-invoke code
  253.  
  254.     delete dialog;
  255. }
  256.  
  257. //----------
  258. void    CEverythingDoc::DoModalRadios ()
  259. {
  260.     DModalRadiosData*        data = new DModalRadiosData;
  261.  
  262.  
  263.     // pre-invoke code
  264.  
  265.     CModalRadios*        dialog = CModalRadios::CreateModalRadios (this, cmdModalRadios, data);
  266. }
  267.  
  268. //----------
  269. void    CEverythingDoc::FinishModalRadios (
  270.     void*        ioParam)
  271. {
  272.     CModalRadios*        dialog = (CModalRadios *)ioParam;
  273.     DModalRadiosData*        data = dialog->GetData ();
  274.  
  275.  
  276.     // post-invoke code
  277.  
  278.     delete dialog;
  279. }
  280.  
  281. //----------
  282. void    CEverythingDoc::DoModalText ()
  283. {
  284.     DModalTextData*        data = new DModalTextData;
  285.  
  286.  
  287.     // pre-invoke code
  288.  
  289.     CModalText*        dialog = CModalText::CreateModalText (this, cmdModalText, data);
  290. }
  291.  
  292. //----------
  293. void    CEverythingDoc::FinishModalText (
  294.     void*        ioParam)
  295. {
  296.     CModalText*        dialog = (CModalText *)ioParam;
  297.     DModalTextData*        data = dialog->GetData ();
  298.  
  299.  
  300.     // post-invoke code
  301.  
  302.     delete dialog;
  303. }
  304.  
  305. //----------
  306. void    CEverythingDoc::DoModalStuff ()
  307. {
  308.     DModalStuffData*        data = new DModalStuffData;
  309.  
  310.  
  311.     // pre-invoke code
  312.  
  313.     CModalStuff*        dialog = CModalStuff::CreateModalStuff (this, cmdModalStuff, data);
  314. }
  315.  
  316. //----------
  317. void    CEverythingDoc::FinishModalStuff (
  318.     void*        ioParam)
  319. {
  320.     CModalStuff*        dialog = (CModalStuff *)ioParam;
  321.     DModalStuffData*        data = dialog->GetData ();
  322.  
  323.  
  324.     // post-invoke code
  325.  
  326.     delete dialog;
  327. }
  328.  
  329. //----------
  330. void    CEverythingDoc::DoModalMoreStuff ()
  331. {
  332.     DModalBarsData*        data = new DModalBarsData;
  333.  
  334.  
  335.     // pre-invoke code
  336.  
  337.     CModalBars*        dialog = CModalBars::CreateModalBars (this, cmdModalMoreStuff, data);
  338. }
  339.  
  340. //----------
  341. void    CEverythingDoc::FinishModalMoreStuff (
  342.     void*        ioParam)
  343. {
  344.     CModalBars*        dialog = (CModalBars *)ioParam;
  345.     DModalBarsData*        data = dialog->GetData ();
  346.  
  347.  
  348.     // post-invoke code
  349.  
  350.     delete dialog;
  351. }
  352.  
  353. //----------
  354. Boolean
  355. CEverythingDoc::ObeyCommand(
  356.     CommandT    inCommand,
  357.     void*        ioParam)
  358. {
  359.     Boolean        cmdHandled = true;
  360.  
  361.     if (inCommand < 0) {
  362.         // modal dialog has finished
  363.  
  364.         switch (-inCommand) {
  365.         case cmdModalButtons:    
  366.                 FinishModalButtons (ioParam);
  367.             break;
  368.  
  369.         case cmdModalCheckboxes:    
  370.                 FinishModalCheckboxes (ioParam);
  371.             break;
  372.  
  373.         case cmdModalRadios:    
  374.                 FinishModalRadios (ioParam);
  375.             break;
  376.  
  377.         case cmdModalText:    
  378.                 FinishModalText (ioParam);
  379.             break;
  380.  
  381.         case cmdModalStuff:    
  382.                 FinishModalStuff (ioParam);
  383.             break;
  384.  
  385.         case cmdModalMoreStuff:    
  386.                 FinishModalMoreStuff (ioParam);
  387.             break;
  388.  
  389.         }
  390.  
  391.     } else {
  392.         switch (inCommand) {
  393.  
  394.     case cmdModalButtons:
  395.             DoModalButtons ();
  396.         break;
  397.  
  398.     case cmdModalCheckboxes:
  399.             DoModalCheckboxes ();
  400.         break;
  401.  
  402.     case cmdModalRadios:
  403.             DoModalRadios ();
  404.         break;
  405.  
  406.     case cmdModalText:
  407.             DoModalText ();
  408.         break;
  409.  
  410.     case cmdModalStuff:
  411.             DoModalStuff ();
  412.         break;
  413.  
  414.     case cmdModalMoreStuff:
  415.             DoModalMoreStuff ();
  416.         break;
  417.  
  418.         default:
  419.                 cmdHandled = LSingleDoc::ObeyCommand(inCommand, ioParam);
  420.             break;
  421.         }
  422.     }
  423.  
  424.     return cmdHandled;
  425. }
  426.  
  427. //----------
  428. void
  429. CEverythingDoc::FindCommandStatus(
  430.     CommandT    inCommand,
  431.     Boolean        &outEnabled,
  432.     Boolean        &outUsesMark,
  433.     Char16        &outMark,
  434.     Str255        outName)
  435. {
  436.     outUsesMark = false;
  437.  
  438.     switch (inCommand) {
  439.  
  440.     // +++ Add cases here for the commands you handle
  441.  
  442.         case cmdModalButtons:
  443.                 outEnabled = true;
  444.             break;
  445.         case cmdModalCheckboxes:
  446.                 outEnabled = true;
  447.             break;
  448.         case cmdModalRadios:
  449.                 outEnabled = true;
  450.             break;
  451.         case cmdModalText:
  452.                 outEnabled = true;
  453.             break;
  454.         case cmdModalStuff:
  455.                 outEnabled = true;
  456.             break;
  457.         case cmdModalMoreStuff:
  458.                 outEnabled = true;
  459.             break;
  460.  
  461.     default:
  462.             LSingleDoc::FindCommandStatus(inCommand, outEnabled,
  463.                                             outUsesMark, outMark, outName);
  464.         break;
  465.     }
  466. }
  467.